package com.shujia.day16.udp.send.file;

import java.io.FileInputStream;
import java.io.IOException;
import java.net.*;

public class Client {
    public static void main(String[] args) throws IOException {
        // TODO 1.从客户端发送一个文件路径的字符串到服务端
        DatagramSocket datagramSocket = new DatagramSocket();
        System.out.println("Socket启动了");
        byte[] bytes = "data/dir2/th2.jpg".getBytes();
        DatagramPacket dataPacket = new DatagramPacket(bytes, bytes.length, InetAddress.getByName("localhost"), 9999);
        datagramSocket.send(dataPacket);
        datagramSocket.close();

        // TODO 2.从指定的文件路径中，读取数据，再将数据发送给服务端

        DatagramSocket datagramSocket2 = new DatagramSocket();
        System.out.println("Socket启动了");

        // TODO 1. 通过IO流方式读取图片数据
        byte[] imageBytes = new byte[1024];
        FileInputStream fileInputStream = new FileInputStream("data/th.jpg");
        int readLen = 0;
        // TODO readLen 表示每次循环读取到的数据长度
        while ((readLen = fileInputStream.read(imageBytes)) > 0){
            //  TODO 根据读取的字节数组和读取长度构建 DatagramPacket 数据包
            DatagramPacket imageDataPacket = new DatagramPacket(imageBytes, readLen, InetAddress.getByName("localhost"), 9998);
            datagramSocket2.send(imageDataPacket);
            System.out.println("数据发送了一部分...");
        }
        System.out.println("数据发送完成..");


        fileInputStream.close();
        datagramSocket.close();


    }
}
